//
//  LFAddClockViewController.m
//  AlarmClock
//
//  Created by LF on 2017/4/26.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFAddClockViewController.h"
#import "LFAlarmClockModel.h"

@interface LFAddClockViewController ()

@property (nonatomic, strong) LFAlarmClockViewModel *alarmVM; /**< ViewModel */

@end

@implementation LFAddClockViewController

#pragma mark - 懒加载
- (LFAlarmClockViewModel *)alarmVM {
    if (!_alarmVM) {
        _alarmVM = [[LFAlarmClockViewModel alloc] init];
    }
    return _alarmVM;
}

- (LFDateSelector *)dateSelector {
    if (!_dateSelector) {
        _dateSelector = [LFDateSelector shareInstance];
    }
    return _dateSelector;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSLog(@"打开数据---%@", file);
    
    [self setupUI];
}

- (void) setupUI {
    
    // 设置items
    self.navigationItem.title = @"添加闹钟";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(submitAction)];
    
    // 设置数据
    self.dateTF.text = _alarmClockModel.date;
    self.timeTF.text = _alarmClockModel.time;
    self.titleTF.text = _alarmClockModel.message_title;
    self.subTitleTF.text = _alarmClockModel.message_subTitle;
    self.bodyTF.text = _alarmClockModel.message_body;
    self.imgTF.text = _alarmClockModel.image;
    self.identityTF.text = _alarmClockModel.identifier;
    self.switchBtn.on = _alarmClockModel.isOn;
}

- (void) submitAction {
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    self.clockDic =  @{
                       @"date":self.dateTF.text,
                       @"time":self.timeTF.text,
                       @"message_title":self.titleTF.text,
                       @"message_subTitle":self.subTitleTF.text,
                       @"message_body":self.bodyTF.text,
                       @"image":self.imgTF.text,
                       @"identifier":self.identityTF.text,
                       @"isOn":@(self.switchBtn.isOn)
                       };
    
    LFAlarmClockModel *alarmModel = [LFAlarmClockModel mj_objectWithKeyValues:self.clockDic];
    
    if (self.alarmClockModel) {
        alarmModel.timeStamp = self.alarmClockModel.timeStamp;
    } else {
        alarmModel.timeStamp = [NSString stringWithFormat:@"%zd",[[NSDate date] timeIntervalSince1970]];
    }
    
    [LFAddClockTool registerNotification:alarmModel];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

// 选择日期
- (IBAction)dateSelect:(id)sender {
    
    self.dateSelector.mode = UIDatePickerModeDate;
    [[LFDateSelector shareInstance] lf_showDateSelectorInViewController:self withResultBlock:^(NSString *dateString) {
        self.dateTF.text = dateString;
    }];
}

// 选择时间
- (IBAction)timeSelect:(id)sender {
    self.dateSelector.mode = UIDatePickerModeTime;
    [[LFDateSelector shareInstance] lf_showDateSelectorInViewController:self withResultBlock:^(NSString *dateString) {
        self.timeTF.text = dateString;
    }];
}

@end
